﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using org.se.fs.presenter.IView.Template;
using org.se.fs.presenter.Library;
using org.se.fs.presenter.Task.Template;

namespace org.se.fs.presenter.View.Loan
{
    public class RegularLoanLists : Template.ViewLists
    {
        public RegularLoanLists(IFormLists view) : this(view, new Task.Context.RegularLoan()) { }

        public RegularLoanLists(IFormLists view, Crud task)
        {
            View = view;
            Task = task;
        }

        protected override void InitializeSearchOption()
        {
            SearchOption = new List<SearchOption>
                                {
                                    new SearchOption("Code", "code", typeof (string)),
                                    new SearchOption("Description", "description", typeof (string)),
                                    new SearchOption("Regular Amount", "regular_amount", typeof (decimal)),
                                    new SearchOption("Regular Interest", "regular_interest", typeof (decimal)),
                                    new SearchOption("Extended Interest", "extended_interest", typeof (decimal)),
                                    new SearchOption("Principal Amount", "principal_amount", typeof (decimal)),
                                    new SearchOption("Service Fee", "service_fee", typeof (decimal)),
                                    new SearchOption("Reloan", "is_re_loan", typeof (bool)),
                                    new SearchOption("Posted", "is_posted", typeof (bool))
                                };
            var type = (SearchOption.Select(a => a.Option)).ToArray();
            View.FillType(type);
            View.IndexType = -1;
        }

        public override void DisplayData()
        {
            View.StatusLists = string.Format("Result(s): {0} found", Lists.Rows.Count);

            if (Lists.Rows.Count.Equals(0))
                View.Lists = new DataTable();

            SetStatusPage();
        
            var a = (Lists.AsEnumerable().OrderBy(b => b.Field<DateTime>("created_date")));
            Lists = a.CopyToDataTable();

            var d = (Lists.AsEnumerable().Select(c => new
                                                          {
                                                              Code = c.Field<string>("code"),
                                                              Description = c.Field<string>("description"),
                                                              RegAmount = Common.FormatNumeric(c.Field<decimal>("regular_amount")),
                                                              RegInterest = Common.FormatNumeric(c.Field<decimal>("regular_interest")) + "%",
                                                              ExtInterest = Common.FormatNumeric(c.Field<decimal>("extended_interest")) + "%",
                                                              PrincipalAmount = Common.FormatNumeric(c.Field<decimal>("principal_amount")),
                                                              Fee = Common.FormatNumeric(c.Field<decimal>("service_fee")),
                                                              Reloan = c.Field<bool>("is_re_loan") ? "YES" : "NO",
                                                              Posted = c.Field<bool>("is_posted") ? "YES" : "NO"
                                                          })).ToArray();
            View.Lists = d;
        }
    }
}
